package com.byb.server.service.impl;

import com.byb.server.service.ServiceInvService;
import com.byb.server.domain.ServiceInv;
import com.byb.server.repository.ServiceInvRepository;
import com.byb.server.repository.search.ServiceInvSearchRepository;
import com.byb.server.service.dto.ServiceInvDTO;
import com.byb.server.service.mapper.ServiceInvMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing ServiceInv.
 */
@Service
@Transactional
public class ServiceInvServiceImpl implements ServiceInvService{

    private final Logger log = LoggerFactory.getLogger(ServiceInvServiceImpl.class);

    private final ServiceInvRepository serviceInvRepository;

    private final ServiceInvMapper serviceInvMapper;

    private final ServiceInvSearchRepository serviceInvSearchRepository;
    public ServiceInvServiceImpl(ServiceInvRepository serviceInvRepository, ServiceInvMapper serviceInvMapper, ServiceInvSearchRepository serviceInvSearchRepository) {
        this.serviceInvRepository = serviceInvRepository;
        this.serviceInvMapper = serviceInvMapper;
        this.serviceInvSearchRepository = serviceInvSearchRepository;
    }

    /**
     * Save a serviceInv.
     *
     * @param serviceInvDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public ServiceInvDTO save(ServiceInvDTO serviceInvDTO) {
        log.debug("Request to save ServiceInv : {}", serviceInvDTO);
        ServiceInv serviceInv = serviceInvMapper.toEntity(serviceInvDTO);
        serviceInv = serviceInvRepository.save(serviceInv);
        ServiceInvDTO result = serviceInvMapper.toDto(serviceInv);
        //serviceInvSearchRepository.save(serviceInv);
        return result;
    }

    /**
     *  Get all the serviceInvs.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<ServiceInvDTO> findAll(Pageable pageable) {
        log.debug("Request to get all ServiceInvs");
        return serviceInvRepository.findAll(pageable)
            .map(serviceInvMapper::toDto);
    }

    /**
     *  Get one serviceInv by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public ServiceInvDTO findOne(Long id) {
        log.debug("Request to get ServiceInv : {}", id);
        ServiceInv serviceInv = serviceInvRepository.findOne(id);
        return serviceInvMapper.toDto(serviceInv);
    }

    /**
     *  Delete the  serviceInv by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete ServiceInv : {}", id);
        serviceInvRepository.delete(id);
       // serviceInvSearchRepository.delete(id);
    }

    /**
     * Search for the serviceInv corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<ServiceInvDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of ServiceInvs for query {}", query);
        Page<ServiceInv> result = serviceInvSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(serviceInvMapper::toDto);
    }

    /**
     *  根据产品id 查询服务信息
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Set<ServiceInvDTO> findAllByProductId(Long id) {
        return serviceInvRepository.findAllByProductId(id).stream().map(serviceInvMapper::toDto).collect(Collectors.toSet());
    }
}
